<?php
namespace RMD\AccountBundle\EventListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;

class TablePrefix
{
	protected $container;
	protected $tables;

	public function __construct(ContainerInterface $container, $tables)
	{
		$this->container = $container;
		$this->tables = $tables;
	}

	public function loadClassMetadata(LoadClassMetadataEventArgs $args)
	{
		$metadata = $args->getClassMetadata();
		$tableName = $metadata->getTableName();
		if (in_array($tableName, $this->tables)) {
			if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
				$metadata->setTableName($this->container->get('security.context')->getToken()->getUser()->getId() . '_' . $tableName);
			}
		}
	}
}
